** Code for Saia replication + concerns table
** (Table C1)

// saia_simul_vote, sia_simul_vote_`s': prepared from saia_simulation.do
// Save them in RAM for faster loading
use "$path_pch/Data/intermediate/Saia/saia_simul_vote.dta", clear
tempfile saia_simul_vote
save `saia_simul_vote'

foreach s in U L F B {
	use "$path_pch/Data/intermediate/Saia/saia_simul_vote_`s'.dta", clear
	tempfile saia_simul_vote_`s'
	save `saia_simul_vote_`s''
}

// Get neighbor info from here for specific RI
/* ?_MP_id ?_party_id */
local r = 0
use "$path_pch/Data/intermediate/Seating/seating_MP_frontback.dta", clear
keep MP_id session_id party_id ?_MP_idRI`r' ?_party_idRI`r' seat_initRI`r'
foreach var of varlist *RI`r' {
	local newname = subinstr("`var'","RI`r'","",1)
	ren `var' `newname'
}
ren seat_init seat
// Control: proportion of other party peers
g num_peers = (U_MP_id!=.) + (L_MP_id!=.) + (F_MP_id!=.) + (B_MP_id!=.)
g num_othparty_peers = (U_party_id!=party_id & U_MP_id!=. & party_id!=.) + (L_party_id!=party_id & L_MP_id!=. & party_id!=.) + ///
	(F_party_id!=party_id & F_MP_id!=. & party_id!=.) + (B_party_id!=party_id & B_MP_id!=. & party_id!=.)
g prop_othparty_peers = num_othparty_peers/num_peers
assert prop_othparty_peers>=0 & prop_othparty_peers<=1

// FE: PartyXSession
egen PartyXSession = group(party_id session_id)
egen MPXSession = group(MP_id session_id)
drop num_peers num_othparty_peers
tempfile neighborInfo
save `neighborInfo'

// Bring original vote data, get neighbor info
use `saia_simul_vote', replace
merge n:1 MP_id session_id using `neighborInfo', keep(3) nogen //12 MPs never appearing in voting data

// Get neighbor vote, neighbor's party leader vote from saved files.
foreach s in U L F B {
	merge n:1 vote_id `s'_MP_id using `saia_simul_vote_`s'', keep(1 3) nogen
}

// Dependent variable
g non_compliance = vote!=chairman_vote if vote!="" & !inlist(chairman_vote,"","unknown")

// Treatment (divergent_peers)
g denom = (U_vote!="") + (L_vote!="") + (F_vote!="") + (B_vote!="") if chairman_vote!=""
g divergent_peers = (U_vote!=chairman_vote & U_vote!="") + (L_vote!=chairman_vote & L_vote!="") + ///
	(F_vote!=chairman_vote & F_vote!="") + (B_vote!=chairman_vote & B_vote!="") if chairman_vote!=""
replace divergent_peers = divergent_peers/denom

// Instrument (divergent_peers_iv)
g divergent_peers_iv = (U_chairman_vote!=chairman_vote & U_chairman_vote!="") + (L_chairman_vote!=chairman_vote & L_chairman_vote!="") + ///
	(F_chairman_vote!=chairman_vote & F_chairman_vote!="") + (B_chairman_vote!=chairman_vote & B_chairman_vote!="") if chairman_vote!=""
replace divergent_peers_iv = divergent_peers_iv/denom
drop denom			

// Sample restriction
g sample1 = !mi(non_compliance) & (MP_id != chairman_id)
g sample2 = sample1 & !inlist(chairman_id,U_MP_id,L_MP_id,F_MP_id,B_MP_id)
g sample3 = sample2
forv k = 1/19 {
	replace sample3 = 0 if !mi(party`k'_chairman_id) & inlist(party`k'_chairman_id,U_MP_id,L_MP_id,F_MP_id,B_MP_id)
}

// More variables
foreach x in no yes abstain absent {
	gen chair_`x' = chairman_vote == "`x'"
}
egen PartyXVote = group(vote_id party_id)

save "$path_pch/Data/analysis/saia_table_analysisfile", replace


*****************
/* Regressions */
*****************

local filename "saiatable"
local noteWidth "1"
local tex ""


// Regressions prep
local varNum = 7
local footnoteSize = `varNum'+1


/* Panel A */

// initialize panel-specific lists
local basicLists "RList pattern" //lists updated outside of loop
local rowLists "obs" //lists updated inside loop
foreach l in `basicLists' `rowLists' {
	local `l'A ""
}

// Regressions
forv k = 1/`varNum' {
	if `k' == 1 xi: reghdfe non_compliance i.seat i.PartyXSession prop_othparty_peers ///
	(divergent_peers = divergent_peers_iv) if sample3, ///
	absorb(MP_id vote_id) cluster(MPXSession) old
	if inrange(`k',2,5) {
		local kn = `k'-1
		local x: word `kn' of no yes abstain absent
		xi: reghdfe chair_`x' i.seat i.PartyXSession prop_othparty_peers ///
			(divergent_peers = divergent_peers_iv) if sample3, ///
			absorb(MP_id vote_id) cluster(MPXSession) old
	}
	if `k' == 6 xi: reghdfe non_compliance i.seat i.PartyXSession prop_othparty_peers chair_no chair_yes chair_abstain chair_absent ///
	(divergent_peers = divergent_peers_iv) if sample3, ///
	absorb(MP_id vote_id) cluster(MPXSession) old
	if `k' == 7 xi: reghdfe non_compliance i.seat i.PartyXSession prop_othparty_peers ///
	(divergent_peers = divergent_peers_iv) if sample3, ///
	absorb(MP_id PartyXVote) cluster(MPXSession) old
	
	est sto R`k'
	local RListA "`RListA'R`k' "
	local obs = e(N)
	
	// Other table details
	if inlist(`k',1,2,6) local patternA "`patternA'1 "
	else local patternA "`patternA'0 "
	
	// Update list
	foreach l in `rowLists' {
		local `l'A "``l'A' & ``l''"
	}
}

// Check lists
foreach l in `basicLists' `rowLists' {
	dis "``l'A'"
}

// Create panel basis
estout `RListA' using "$path_pch/Output/`filename'C1A.tex", style(tex) replace ///
cells(b(star fmt(%9.2f)) se(nostar fmt(%9.2f) par)) ///
nolabel collabels(none) mlabels(none) ///
keep(divergent_peers) order(divergent_peers) ///
varlabels(divergent_peers "Divergent Peers") ///
starlevels(* 0.1 ** 0.05 *** 0.01)

dis "`RList'"

/* Export */

local tex "`tex' \input{`filename'C1A} \vspace{-1.2em} \\ \hline"
local tex "`tex' MP FEs & Y & Y & Y & Y & Y & Y & Y \\"
local tex "`tex' Seat FEs & Y & Y & Y & Y & Y & Y & Y \\"
local tex "`tex' Peers' Parties $\neq$ MP i's party & Y & Y & Y & Y & Y & Y & Y \\"
local tex "`tex' Voting Procedure FEs & Y & Y & Y & Y & Y & Y & (implicit) \\"
local tex "`tex' Party $\times$ Session FEs & Y & Y & Y & Y & Y & Y & (implicit) \\"
local tex "`tex' Party Leader Vote FEs & N & N & N & N & N & YES & (implicit) \\"
local tex "`tex' Party $\times$ Voting Procedure FEs & N & N & N & N & N & N & YES \\"
local tex "`tex' MP $\times$ Topic FEs & N & N & N & N & N & N & N \\"
local tex "`tex' Observations `obsA' \\ \hline"
local tex "`tex' \toprule"
local tex "`tex' \multicolumn{`footnoteSize'}{p{`noteWidth'\textwidth}}{\footnotesize Notes:"
local tex "`tex' Each column in this table originates from a separate 2SLS regression."
local tex "`tex' Non-compliance is a dummy variable indicating that the MP voted differently to their party leader for the particular voting procedure."
local tex "`tex' In Columns 2-5, the dependent variable is dummy variable indicating the vote of the MP's party leader."
local tex "`tex' Standard errors clustered at MP-session-level. *** p<0.01, ** p<0.05, * p<0.1.} \\ \end{tabular} }"

esttab `RListA' using "$path_pch/Output/`filename'C1.tex", style(tex) booktabs replace ///
d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
mgroups("\specialcell{Col.3\\replication}" "Party leader vote" "\specialcell{Col.3 with\\appropriate FEs}", pattern(`patternA') ///
prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Non-\\compl-\\iance\\(1)}" "\specialcell{\\\\No\\(2)}" ///
	"\specialcell{\\\\Yes\\(3)}" "\specialcell{\\\\Abstain\\(4)}" ///
	"\specialcell{\\\\Absent\\(5)}" "\specialcell{Non-\\compl-\\iance\\(6)}" "\specialcell{Non-\\compl-\\iance\\(7)}")

